其他
关于嵌入式 Qt 最全最棒的教程(万字干货)
The following article is from ZLG致远电子 Author 致远电子
1 /* quitButton.cpp */ 2 #include <QApplication> /* 包含类QApplication的定义 */ 3 #include <QPushButton> /* 包含类QPushButton的定义 */ 4 int main(int argc, char *argv[]) 5 { 6 QApplication app(argc, argv); 7 QPushButton *button = new QPushButton("Quit") ; /*创建一个显示为Quit的按钮窗口部件*/ 8 button->show(); /* 使按钮可见 */ 9 return app.exec(); 10 }
$ qmake-qt4 –project $ qmake-qt4 $ make
1 /* account.cpp */ 2 #include <QApplication> 3 #include <QLabel> 4 #include <QLineEdit> 5 #include <QHBoxLayout> 6 7 intmain(int argc, char *argv[]) 8 { 9 QApplication app(argc, argv); 10 QWidget *window = new QWidget; /* 创建Qwidget对象,作为主窗口*/ 11 window->setWindowTitle("Enter Your account"); /*设置显示在窗口标题栏上的文字*/ 12 13 QLabel *accountLabel = new QLabel("Account: ", window); /*创建标签对象,并显示为Accoutn */ 14 QLineEdit *accountEdit = new QLineEdit(window); /* 创建文本框对象 */ 15 16 QHBoxLayout *layout = new QHBoxLayout; /* 创建水平布局管理器 */ 17 layout->addWidget(accountLabel); 18 layout->addWidget(accountEdit); 19 20 window->setLayout(layout); /* window窗口上安装该布局管理*/ 21 window->show(); 22 return app.exec(); 23 }
$ qmake-qt4 –project $ qmake-qt4 $ make
1、QHBoxLayout:在水平方向上排列窗口部件,从左到右; 2、QVBoxLayout:在垂直方向上排列窗口部件,从上到下; 3、QGridLayout:把各个窗口部件排列在一个网格中。
1 /* layout.cpp */ 2 #include <QtGui> 3 #include <QApplication> 4 5 int main(int argc, char *argv[]) 6 { 7 QApplication app(argc, argv); 8 QWidget *window = new QWidget; 9 window->setWindowTitle("Layout"); 10 11 QLabel *accountLabel = new QLabel("Account: "); 12 QLabel *pwLabel = new QLabel("Password: "); 13 QLineEdit *accountEdit = new QLineEdit; 14 QLineEdit *pwEdit = new QLineEdit; 15 QPushButton *quitButton = new QPushButton("Quit"); 16 QPushButton *nextButton = new QPushButton("Next"); 17 18 QHBoxLayout *topLayout = new QHBoxLayout; /* 创建一个水平布局管理器 */ 19 topLayout->addWidget(accountLabel); /* 从左到右放置窗口部件 */ 20 topLayout->addWidget(accountEdit); 21 22 QHBoxLayout *downLayout = new QHBoxLayout; 23 downLayout->addWidget(pwLabel); 24 downLayout->addWidget(pwEdit); 25 26 QVBoxLayout *leftLayout = new QVBoxLayout; /* 创建一个垂直布局管理器 */ 27 leftLayout->addLayout(topLayout); /* 内嵌布局管理器 */ 28 leftLayout->addLayout(downLayout); 29 leftLayout->addStretch(); /* 添加分隔符 */ 30 31 QVBoxLayout *rightLayout = new QVBoxLayout; /* 创建一个垂直布局管理器 */ 32 rightLayout->addWidget(quitButton); /* 从上到下放置窗口部件 */ 33 rightLayout->addWidget(nextButton); 34 rightLayout->addStretch(); 35 36 QHBoxLayout *mainLayout = new QHBoxLayout; 37 mainLayout->addLayout(leftLayout); 38 mainLayout->addLayout(rightLayout); 39 40 QObject::connect(quitButton, SIGNAL(clicked()), &app, SLOT(quit())); 41 window->setLayout(mainLayout); 42 window->show(); 43 return app.exec(); 44 }
$ qmake-qt4 –project $ qmake-qt4 $ make
connect(sender, SIGNAL(signal), receiver, SLOT(slot));
connect(slider, SIGNAL(valueChanged(int)), spinBox, SLOT(setValue(int))); connect(slider, SIGNAL(valueChanged(int)), this, SLOT(updateStatusBar(int)));
connect(lcd, SIGNAL(over()), this, SLOT(handleError()); connect(calculator, SIGNAL(divisionError()), this, SLOT(handleError()));
connect(lineEdit, SIGNAL(textChanged(Qstring &)), this, SIGNAL(update(Qstring &)));
disconnect(lcd, SIGNAL(over()), this, SLOT(handleError()));
connect(ftp, SIGNAL(rawReply(int, const Qstring &)), this, SLOT(checkError(int)));
1 /* quitButton.cpp */ 2 #include <QApplication> 3 #include <QPushButton> 4 int main(int argc, char *argv[]) 5 { 6 QApplication app(argc, argv); 7 QPushButton *button = new QPushButton("Quit"); 8 QObject::connect(button, SIGNAL(clicked()),&app, SLOT(quit())); 9 button->show(); 10 return app.exec(); 11 }
1 /* number.cpp */ 2 #include <QApplication> 3 #include <QHBoxLayout> 4 #include <QSlider> 5 #include <QSpinBox> 6 7 int main(int argc, char *argv[]) 8 { 9 QApplication app(argc, argv); 10 QWidget *window = new QWidget; 11 window->setWindowTitle("Enter Number"); 12 13 QSpinBox *spinBox = new QSpinBox; /* 创建微调框 */ 14 QSlider *slider = new QSlider(Qt::Horizontal); /* 创建滑块 */ 15 spinBox->setRange(0, 100); /* 设置微调框有效范围 */ 16 slider->setRange(0, 100); /* 设置滑块有效范围 */ 17 18 QObject::connect(spinBox, SIGNAL(valueChanged(int)),slider, SLOT(setValue(int))); 19 QObject::connect(slider, SIGNAL(valueChanged(int)),spinBox, SLOT(setValue(int))); 20 spinBox->setValue(25); /* 设置微调框的值为25 */ 21 22 QHBoxLayout *layout = new QHBoxLayout; /* 创建水平布局管理器 */ 23 layout->addWidget(spinBox); /* 从左到右放置部件 */ 24 layout->addWidget(slider); 25 window->setLayout(layout); 26 27 window->show(); 28 29 return app.exec(); 30 }
$ qmake-qt4–project $ qmake-qt4 $ make
1 /* mainwindow.h */ 2 #ifndef MAINWINDOW_H 3 #define MAINWINDOW_H 4 5 #include <QMainWindow> /* 包含对QMainWindow的定义 */ 6 7 class MainWindow : public QMainWindow /* 声明MainWindow为QMainWindow的子类*/ 8 { 9 Q_OBJECT; 10 public: 11 MainWindow(void); 12 ~MainWindow(void); 13 }; 14 15 #endif
1 /* mainwindow.cpp */ 2 3 #include "mainwindow.h" /* 包含mainwindow.h头文件 */ 4 5 MainWindow::MainWindow(void) 6 { 7 } 8 9 MainWindow::~MainWindow(void) 10 { 11 }
1 /* main.cpp */23 #include <QApplication>4 #include "mainwindow.h"56 int main(int argc, char *argv[])7 {8 QApplication app(argc, argv);9 MainWindow main; /* 创建自定义的MainWindow对象 */10 main.show(); /* 使对象显示 */11 return app.exec();12 }
$ qmake-qt4 –project $ qmake-qt4 $ make
1 /* mainwindow.h */ 2 #ifndef MAINWINDOW_H 3 #define MAINWINDOW_H 4 5 #include <QMainWindow> /* 包含对QMainWindow的定义 */ 6 7 class QAction; 8 class QMenu; 9 class QToolBar; 10 class QLabel; 11 12 class MainWindow : public QMainWindow /* 声明MainWindow为QMainWindow的子类*/ 13 { 14 Q_OBJECT; 15 public: 16 MainWindow(void); 17 ~MainWindow(void); 18 19 private: 20 QMenu *fileMenu; /* 文件菜单 */ 21 QMenu *helpMenu; /* 帮助菜单 */ 22 23 QToolBar *fileToolBar; /* 工具栏 */ 24 25 QAction *openAction; /* 打开动作 */ 26 QAction *closeAction; /* 关闭动作 */ 27 QAction *aboutAction; /* 关于动作 */ 28 29 QLabel *statusLabel; 30 }; 31 32 #endif
1 /* mainwindow.cpp */ 2 3 #include <QtGui> 4 #include "mainwindow.h" 5 6 MainWindow::MainWindow(void) 7 { 8 openAction = new QAction(tr("&Open"), this); /* 创建打开动作 */ 9 openAction->setStatusTip(tr("Open the file")); /* 设置状态提示 */ 10 11 closeAction = new QAction(tr("&Close"), this); /* 创建关闭动作 */ 12 closeAction->setStatusTip(tr("Close the file")); /* 设置状态提示 */ 13 14 aboutAction = new QAction(tr("&About"), this); /* 创建关于动作 */ 15 aboutAction->setStatusTip(tr("About")); /* 设置状态提示 */ 16 17 fileMenu = menuBar()->addMenu(tr("&File")); /* 创建file菜单 */ 18 fileMenu->addAction(openAction); 19 fileMenu->addAction(closeAction); 20 21 helpMenu = menuBar()->addMenu(tr("&Help")); /* 创建help菜单 */ 22 helpMenu->addAction(aboutAction); 23 24 fileToolBar = addToolBar(tr("&File")); /* 创建File工具栏 */ 25 fileToolBar->addAction(openAction); /* 添加打开动作到File工具栏*/ 26 fileToolBar->addAction(closeAction); /* 添加关闭动作到工具栏 */ 27 fileToolBar->addAction(aboutAction); /* 添加关于动作到工具栏 */ 28 29 statusLabel = new QLabel; 30 statusLabel->setMinimumSize(statusLabel->sizeHint()); 31 statusLabel->setAlignment(Qt::AlignHCenter); /* 设置居中 */ 32 33 statusBar()->addWidget(statusLabel); /* 添加状态标签到状态栏 */ 34 } 35 36 MainWindow::~MainWindow(void) 37 { 38 }
$ qmake-qt4 –project $ qmake-qt4 $ make
1 /* coordinate_label.h */ 2 3 #ifndef COORDINATE_LABEL_H 4 #define COORDINATE_LABEL_H 5 6 #include <QLabel> 7 8 class QMouseEvent; 9 10 class CoordinateLabel : public QLabel /* 自定义QLabel的子类 */ 11 { 12 protected: 13void mousePressEvent(QMouseEvent *event); /* 定义鼠标按下事件 */ 14void mouseMoveEvent(QMouseEvent *event); /* 定义鼠标移动事件 */ 15void mouseReleaseEvent(QMouseEvent *event); /* 定义鼠标释放事件 */ 16 }; 17 18 #endif
1 /* coordinate_label.cpp */ 2 3 #include <QMouseEvent> 4 #include "coordinate_label.h" 5 6 void CoordinateLabel::mousePressEvent(QMouseEvent *event) 7 { 8 this->setText(QString("Mouse Press at:[%1, %2]") 9 .arg(event->x()) 10 .arg(event->y())); 11 } 12 13 void CoordinateLabel::mouseMoveEvent(QMouseEvent *event) 14 { 15 this->setText(QString("Mouse Move at:[%1, %2]") 16 .arg(event->x()) 17 .arg(event->y())); 18 } 19 20 void CoordinateLabel::mouseReleaseEvent(QMouseEvent *event) 21 { 22 this->setText(QString("Mouse Release at:[%1, %2]") 23 .arg (event->x()) 24 .arg(event->y())); 25 }
1 /* main.cpp */ 2 3 #include <QApplication> 4 #include "coordinate_label.h" 5 6 int main(int argc, char *argv[]) 7 { 8 QApplication app(argc, argv); 9 CoordinateLabel *myLabel = new CoordinateLabel; 10 myLabel->setWindowTitle("Mouse Event Demo"); /* 设置窗口标题 */ 11 myLabel->resize(300,200); /* 设置自定义的窗口部件尺寸*/ 12 myLabel->show(); 13 return app.exec(); 14 }
$ qmake-qt4 –project $ qmake-qt4 $ make
-END-
推荐阅读
【01】在单片机(MCU)上运行Qt?Qt for MCUs 1.0正式发布【02】关于QT,你需要知道这些基础知识【03】嵌入式Linux GUI的新选择:开源!免费!高效!易用!【04】几种常用的嵌入式Linux GUI及其特点【05】嵌入式 Linux QT 编程入门